package com.example.xln111.Dao;

import com.example.xln111.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Student> getAll(){
        return jdbcTemplate.query("select * from student",new BeanPropertyRowMapper<>(Student.class));
    }

    public int addStudent(Student student){
        return jdbcTemplate.update("INSERT INTO student(name,sex,classname) VALUES(?,?,?)",student.getName(),student.getSex(),student.getClassname());
    }

    public int updateStudent(Student student){
        return jdbcTemplate.update("UPDATE student SET name=?,sex=?,classname=? WHERE id=?",
                student.getName(),student.getSex(),student.getClassname());
    }

    public int deleteStudentById(int id){
        return jdbcTemplate.update("DELETE FROM student WHERE id=?",id);
    }

    public Student getStudentById(int id){
        return jdbcTemplate.queryForObject("select * from student where id = ?",new BeanPropertyRowMapper<>(Student.class),id);
    }


}
